﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditGameThing : BaseDataVisualizer, ISingleItemDataVisualizer
    {
        public override void SetVisualizedData(DataVisualizer vis, object o) { GameThing = o; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(object); } }
        public string VisualizerName { get { return "EditGameThing"; } }

        public static readonly DependencyProperty GameThingProperty = DependencyProperty.Register(
            "GameThing", typeof(object), typeof(EditGameThing), new PropertyMetadata(null, new PropertyChangedCallback(GameThingChanged)));

        public object GameThing
        {
            get { return (object)GetValue(GameThingProperty); }
            set { SetValue(GameThingProperty, value); }
        }

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<EditGameThing, T2> action)
        {
            d.HandleChange<EditGameThing, T2>(e, action, (t) => t.UpdateBindings());
        }

        private static void GameThingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            EditGameThing t = (EditGameThing)d;
            t.GameThing = e.NewValue;
            t.UpdateBindings();
        }

        public override void UpdateBindings()
        {
            this.DataContext = GameThing;

            if (GameThing != null && (GameThing as GameThingBaseClass) != null && (GameThing as GameThingBaseClass).States == null)
            {
                showStates.Visibility = Visibility.Collapsed;
            }
        }

        public EditGameThing()
        {
            InitializeComponent();
            SizeChanged += new SizeChangedEventHandler(EditGameThing_SizeChanged);
        }

        void EditGameThing_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            
        }

        private void EditNameInfo_Loaded(object sender, RoutedEventArgs e)
        {
            EditNameInfo nameInfo = (sender as DataVisualizer).Visualizer as EditNameInfo;

            if (nameInfo == null)
                return;

            //nameInfo.Window.Indent = 1;
        }

        private void EditProperties_Loaded(object sender, RoutedEventArgs e)
        {
            ShowPropertiesControl propertiesControl = sender as ShowPropertiesControl;

            if (propertiesControl == null)
                return;

            //propertiesControl.Window.Indent = 1;
        }
        
        private void EditAbilities_Loaded(object sender, RoutedEventArgs e)
        {
            ShowAbilitiesControl showAbilities = (sender as DataVisualizer).Visualizer as ShowAbilitiesControl;

            if (showAbilities == null)
                return;

            if (!(GameThing is GameThingBaseClass))
            {
                showAbilities.Visibility = Visibility.Collapsed;
                showAbilities.Abilities = null;
            }
            //showAbilities.Window.Indent = 1;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!typeof(GameChar).IsAssignableFrom(this.DataContext.GetType()))
                showAbilities.Visibility = Visibility.Collapsed;
        }

        public override string TitleText
        {
            get
            {
                if (this.DataContext != null)
                {
                    BaseClass baseClass = this.DataContext as BaseClass;
                    GameThingBaseClass thing = baseClass as GameThingBaseClass;

                    string typeName = string.Empty;

                    if (thing == null)
                        typeName = baseClass.GetType().Name;
                    else
                        typeName = thing.ThingType.Name;

                    return string.Format("{0} {1}, Keywords: {2}", typeName, baseClass.ShortName, baseClass.Keywords == null ? "None" : baseClass.Keywords).Trim();
                }
                return base.TitleText;
            }
        }

        public override void OnSaveData()
        {
            DataAccess.StoreThingAsync(this.DataContext);

            //if (this.DataContext is GameChar)
            //{
            //    DataAccess.Client.StoreGameCharAsync(DataAccess.AssignedUniqueId, this.DataContext as GameChar);
            //}
            //else if (this.DataContext is GameCharTemplate)
            //{
            //    DataAccess.Client.StoreGameCharTemplateAsync(DataAccess.AssignedUniqueId, this.DataContext as GameCharTemplate);
            //}
            //else if (this.DataContext is Ability)
            //{
            //    DataAccess.Client.StoreAbilityAsync(DataAccess.AssignedUniqueId, this.DataContext as Ability);
            //}
            //else if (this.DataContext is State)
            //{
            //    DataAccess.Client.StoreStateAsync(DataAccess.AssignedUniqueId, this.DataContext as State);
            //}
            //else if (this.DataContext is GameLocation)
            //{
            //    DataAccess.Client.StoreGameLocationAsync(DataAccess.AssignedUniqueId, this.DataContext as GameLocation);
            //}
        }

        private void showStates_Loaded(object sender, RoutedEventArgs e)
        {
            ShowStatesControl states = (sender as DataVisualizer).Visualizer as ShowStatesControl;

            if (this.DataContext == null || (this.DataContext as GameThingBaseClass) == null || (this.DataContext as GameThingBaseClass).States == null)
            {
                showStates.Visibility = Visibility.Collapsed;
                showStates.DataToVisualize = null;
            }

            if (states == null)
                return;

            //states.Window.Indent = 1;
        }

        private void EditTemplateID_RequestEditTemplate(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnRequestEditID(sender, e);
        }        
    }
}
